package com.platform.controller;

import java.util.*;

import com.platform.entity.*;
import com.platform.service.ApiLabelService;
import com.platform.service.ApiUserLabelService;
import com.platform.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.platform.service.CommentService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;

/**
 * Controller
 *
 * @author zhurusen
 * @email 524227263@qq.com
 * @date 2018-07-01 18:01:57
 */
@Controller
@RequestMapping("comment")
public class CommentController {
    @Autowired
    private CommentService apiCommentService;
    @Autowired
    private UserService userService;
    @Autowired
    private ApiUserLabelService apiUserLabelService;
    @Autowired
    private ApiLabelService labelService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("comment:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<CommentEntity> list = apiCommentService.queryList(query);

        List<Map<String, Object>> reList = new ArrayList<>();
        for (CommentEntity info : list) {
            Map<String, Object> map = new HashMap<>();

            map.put("id", info.getId());
            map.put("createDate", info.getCreateDate());
            map.put("content", info.getContent());

            UserEntity userVo = userService.queryObject(info.getCommentUserId());
            if (userVo != null) {
                map.put("userName", userVo.getNickname());
            } else {
                map.put("userName", "-");
            }
            Map<String, Object> pram = new HashMap<>();
            pram.put("tvAnchor", info.getCommentUserId());

            List<ApiUserLabelVo> userLabelVos = apiUserLabelService.selectList(pram);

            String labels = "";
            for (ApiUserLabelVo infos : userLabelVos) {
                labels += infos.getLabelName() + "   ";
            }

            map.put("labels", labels);
            reList.add(map);
        }


        int total = apiCommentService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(reList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("comment:info")
    @ResponseBody
    public R info(@PathVariable("id") Long id) {
        CommentEntity apiComment = apiCommentService.queryObject(id);

        List<Long> lableIdList = new ArrayList<>();

        Map<String, Object> pram = new HashMap<>();
        pram.put("mouldId", apiComment.getOrderId());
        pram.put("mould_name", "live");

        List<ApiUserLabelVo> userLabelVos = apiUserLabelService.selectList(pram);

        for (ApiUserLabelVo infos : userLabelVos) {
            lableIdList.add(infos.getId());
        }
        apiComment.setLableIdList(lableIdList);

        return R.ok().put("comment", apiComment);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("comment:save")
    @ResponseBody
    public R save(@RequestBody CommentEntity apiComment) {
        apiComment.setCreateDate(new Date());
        apiComment.setUserId(105L);
        apiComment.setType(1);
        apiCommentService.save(apiComment);

        List<Long> list = apiComment.getLableIdList();
        for (Long info : list) {
            ApiLabelVo apiLabelVo = labelService.selectOne(info);
            ApiUserLabelVo labelVo = new ApiUserLabelVo();
            labelVo.setLabelId(info);
            labelVo.setLabelName(apiLabelVo.getName());
            labelVo.setTvAnchor(apiComment.getCommentUserId());
            labelVo.setUserId(105L);
            apiUserLabelService.save(labelVo);
        }


        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("comment:update")
    @ResponseBody
    public R update(@RequestBody CommentEntity apiComment) {
        apiCommentService.update(apiComment);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("comment:delete")
    @ResponseBody
    public R delete(@RequestBody Long[] ids) {
        apiCommentService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<CommentEntity> list = apiCommentService.queryList(params);

        return R.ok().put("list", list);
    }
}
